<?php

class PaginationControl extends TTemplateControl
{	
	public $offset;
	public $current_site;
	public $all_sites;
	public $elements_per_site;
	
	public $page_name;
	public $page_params;
	
	
	public function onLoad($params)
	{
		parent::onLoad($params);
	}
	
	public function setData($page_name, $page_params, $current_site, $all_elements, $elements_per_site = null)
	{
		if(!$elements_per_site)
			$elements_per_site = 10;
		
		if($current_site == null)
			$current_site = 1;
		if($current_site < 1)
			$current_site = 1;
		$all_sites = ceil($all_elements / $elements_per_site);
		if($current_site > $all_sites)
			$current_site = $all_sites;
		
		$this->offset =  ($current_site - 1)*$elements_per_site;
		
		if($this->offset < 0)
			$this->offset = 0;
		
		$this->first_site_link->Text = $all_elements > 0 ? 1 : 0;
		$this->current_site_link->Text = $current_site;
		$this->all_sites_link->Text = $all_sites;
		$this->page_name = $page_name;
		$this->page_params = $page_params;
		$this->current_site = $current_site;
		$this->all_sites = $all_sites;
		$this->elements_per_site = $elements_per_site;
		
		if($this->all_sites <= 1)
			$this->panel->setVisible(false);
	}
	
	public function getLastSite()
	{
		return $this->Service->constructUrl(
						$this->page_name,
						array_merge($this->page_params, array('site' => $this->all_sites)));
	}
	
	public function getOffset()
	{
		return $this->offset;
	}
	
	public function getElementsPerSite()
	{
		return $this->elements_per_site;
	}
	
	public function previousSiteButtonClicked()
	{
		$previous_site = $this->current_site - 1;
		if($previous_site < 1)
			$previous_site = 1;
		$url = $this->Service->constructUrl(
						$this->page_name,
						array_merge($this->page_params, array('site' => $previous_site)));
		$this->Response->Redirect($url);
	}
	
	public function nextSiteButtonClicked()
	{
		$next_site = $this->current_site + 1;
		if($next_site > $this->all_sites)
			$next_site = $this->all_sites;
		$url = $this->Service->constructUrl(
						$this->page_name,
						array_merge($this->page_params, array('site' => $next_site)));
		$this->Response->Redirect($url);
	}
	
	public function firstSiteButtonClicked()
	{
		$url = $this->Service->constructUrl(
						$this->page_name,
						array_merge($this->page_params, array('site' => 1)));
		$this->Response->Redirect($url);
	}
	
	public function lastSiteButtonClicked()
	{
		$url = $this->Service->constructUrl(
						$this->page_name,
						array_merge($this->page_params, array('site' => $this->all_sites)));
		$this->Response->Redirect($url);
	}
	
	public function getAllSitesCount()
	{
		return $this->all_sites;
	}
}

?>